package com.example.ceramics.fragment;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.ceramics.R;
import com.example.ceramics.activity.SearchActivity;
import com.example.ceramics.adapter.MyPagerAdapter1;
import com.example.ceramics.data.Datas;
import com.example.ceramics.entity.GoodsEntity;
import com.example.ceramics.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class SortFragment extends BaseFragment implements VerticalTabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    private VerticalTabLayout mTab;
    private ViewPager mVp;
    private ArrayList<String> mTitlesList;
    private ArrayList<Fragment> mFragmentsList;
    private ImageView ivSearch;
    private EditText etSearch;
    private ImageView ivSearchSmall;
    private EditText etSearchSmall;

    public SortFragment() {
    }

    public static SortFragment newInstance() {
        SortFragment fragment = new SortFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_sort;
    }

    @Override
    protected void initView() {
        mTab = mRootView.findViewById(R.id.tab);
        mVp = mRootView.findViewById(R.id.vp);

        mTitlesList = new ArrayList<>();//初始化Tab栏数据
        mTitlesList.add("所有商品");
        mTitlesList.add("十二生肖系列");
        mTitlesList.add("阴阳五行系列");
        mTitlesList.add("四季系列");
        mTitlesList.add("百鸟朝凤系列");
        mTitlesList.add("地域风采系列");
        mTitlesList.add("节日限定");
        mTitlesList.add("其他周边");
        mTitlesList.add("定制服务");
        mTitlesList.add("人物形象");

        mFragmentsList = new ArrayList<>();//添加碎片
//        mFragmentsList.add(GoodsFragment.newInstance(mTitlesList.get(0)));
//        mFragmentsList.add(GoodsFragment.newInstance(mTitlesList.get(1)));
//        mFragmentsList.add(GoodsFragment.newInstance(mTitlesList.get(2)));
//        mFragmentsList.add(GoodsFragment.newInstance(mTitlesList.get(3)));
//        mFragmentsList.add(GoodsFragment.newInstance(mTitlesList.get(4)));

//        for (String title : mTitlesList) {
//            mFragmentsList.add(GoodsFragment.newInstance(title));
//        }

        mFragmentsList.add(GoodsFragment.newInstance("所有商品"));
        mFragmentsList.add(GoodsOnlyOneFragment.newInstance("十二生肖系列"));
        mFragmentsList.add(GoodsOnlyOneFragment.newInstance("阴阳五行系列"));
        mFragmentsList.add(GoodsNullFragment.newInstance());
        mFragmentsList.add(GoodsOnlyOneFragment.newInstance("百鸟朝凤系列"));
        mFragmentsList.add(GoodsNullFragment.newInstance());
        mFragmentsList.add(GoodsNullFragment.newInstance());
        mFragmentsList.add(GoodsFragment.newInstance("其他周边"));
        mFragmentsList.add(ServiceFragment.newInstance());
        mFragmentsList.add(GoodsFragment.newInstance("人物形象"));

        //创建适配器、适配器继承自FragmentPagerAdapter
        MyPagerAdapter1 myPagerAdapter = new MyPagerAdapter1(getActivity().getSupportFragmentManager(), mFragmentsList);
        mVp.setOffscreenPageLimit(mFragmentsList.size());
        mVp.setAdapter(myPagerAdapter);//绑定适配器
        //Tab和ViewPager相互联动
        mVp.setOnPageChangeListener(this);
        mTab.addOnTabSelectedListener(this);

        ivSearch = mRootView.findViewById(R.id.iv_search);
        etSearch = mRootView.findViewById(R.id.et_search);
        ivSearchSmall = mRootView.findViewById(R.id.iv_search_small);
        etSearchSmall = mRootView.findViewById(R.id.et_search_small);
    }

    @Override
    protected void initData() {
        mTab.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return mTitlesList.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public QTabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public QTabView.TabTitle getTitle(int position) {
                QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                        .setContent(mTitlesList.get(position))//设置数据   也有设置字体颜色的方法
                        .setTextColor(getResources().getColor(R.color.blue_logo), 0xFF757575)
                        .build();
                return title;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.shape_tab;//选中的背景颜色
            }
        });

        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchWord = etSearch.getText().toString();
                search(searchWord);
            }
        });
        ivSearchSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchWord = etSearchSmall.getText().toString();
                search(searchWord);
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTab.setTabSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabSelected(TabView tab, int position) {
        mVp.setCurrentItem(position);
    }

    @Override
    public void onTabReselected(TabView tab, int position) {

    }

    private void search(String searchWord) {
        if (StringUtils.isEmpty(searchWord)) {
            showToast("请输入想要搜索的内容！");
            return;
        }
        Datas datas = new Datas();
        datas.setDatas();
        List<GoodsEntity> searchGoods = datas.search(searchWord);
        HashMap<String, Object> searchMap = new HashMap<>();
        searchMap.put("list", searchGoods);
        searchMap.put("searchWord", searchWord);
        navigateTo(SearchActivity.class, "searchMap", searchWord);
    }
}